package com.cn.aam.checaiduo.base.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.cn.aam.checaiduo.base.adapter.holder.BaseRecyclerViewHolder;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by 艹羊 on 2017/5/15.
 */

public abstract class BaseVLayoutAdapter<T> extends DelegateAdapter.Adapter<BaseRecyclerViewHolder> {

    protected Context mContext;
    private List<T> dataList = new ArrayList<>();//不让子类直接使用
    protected OnItemClick onItemClick;


    public BaseVLayoutAdapter(Context mContext) {
        this.mContext = mContext;
    }


    //获取Viewholder
    protected abstract BaseRecyclerViewHolder getViewHolder(View picNewsView);


    //获取布局
    protected abstract int getItemLayoutRes();

    //视图变化
    protected abstract void ItemViewChange(BaseRecyclerViewHolder recyclerholder, final int position);

    @Override
    public void onViewAttachedToWindow(BaseRecyclerViewHolder holder) {
        super.onViewAttachedToWindow(holder);
    }

    @Override
    public void onViewDetachedFromWindow(BaseRecyclerViewHolder holder) {
        super.onViewDetachedFromWindow(holder);
        holder.itemView.clearAnimation();
    }

    @Override
    public BaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View picNewsView = null;
        LayoutInflater mInflater = LayoutInflater.from(parent.getContext());
        picNewsView = mInflater.inflate(getItemLayoutRes(), parent, false);//防止item显示不全问题
        return getViewHolder(picNewsView);
    }

    @Override
    public void onBindViewHolder(BaseRecyclerViewHolder holder, int position) {
        ItemViewChange(holder, position);
    }


    public List<T> getDataList() {
        return dataList;
    }


    public void setDataToAdapter(List<T> data) {
        if (data == null) {
            return;
        }
        dataList.clear();
        dataList.addAll(data);
        notifyDataSetChanged();
    }


    public void addData(List<T> data) {
        dataList.addAll(data);
        notifyDataSetChanged();
    }

    public void insertData(T item) {
        this.dataList.add(item);
        this.notifyDataSetChanged();
    }

    /**
     * 删除某个item【带有动画】
     */
    public void removeItem(int position) {
        dataList.remove(position);
        notifyDataSetChanged();
        //this.notifyItemRemoved(position + headViewSize);
        //this.notifyItemRangeChanged(position + headViewSize, getItemCount() - footViewSize - 1);
    }

    /**
     * 删除所有数据
     */
    public void clearAllData() {
        dataList.clear();
        notifyDataSetChanged();
    }

    public T getItem(int position) {
        if (dataList == null || dataList.size() == 0) {
            return null;
        }
        return dataList.get(getItemPosition(position));
    }

    //获取item所对应的position
    protected int getItemPosition(int posstion) {
        return posstion;
    }


    //item的点击接口
    public interface OnItemClick {
        void onClick(int position);
    }

    public void setOnItemClick(OnItemClick onItemClick) {
        this.onItemClick = onItemClick;
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }
}
